package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.EmployeeQueryObject;
import cn.wolfcode.rbac.service.IEmployeeServcie;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.PageData;
import cn.wolfcode.rbac.util.R;
import cn.wolfcode.rbac.web.vo.EmployeeVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
    @Autowired
    private IEmployeeServcie employeeService;
    @Autowired
    private IRoleService roleService;
    @GetMapping
    public R query(EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.selectList(qo);
        return R.ok(new PageData(pageInfo));
    }
    @PostMapping
    public R add(@RequestBody EmployeeVo vo){
        employeeService.add(vo);
        return R.ok();
    }
    @GetMapping("/{id}/roles")
    public R queryRoleByEId(@PathVariable("id") Long id){
        List<Role> roles =  roleService.queryRoleByEId(id);
        return R.ok(roles);
    }
    @PutMapping("/{id}")
    public R update(@PathVariable("id") Long id,@RequestBody EmployeeVo vo){
        employeeService.update(id,vo);
        return R.ok();
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return R.ok();
    }
    @PatchMapping("/{id}")
    public R updateAdminState(@PathVariable("id")Long id,Boolean admin){
        employeeService.updateAdminState(id,admin);
        return R.ok();
    }
}
